package com.redis.demo.rabbitmq.Direct;

import com.rabbitmq.client.*;
import com.redis.demo.rabbitmq.Constants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setUsername(Constants.USERNAME);
        factory.setPassword(Constants.PASSWORD);
        factory.setVirtualHost(Constants.VIRTUALHOST);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(Constants.DIRECT_QUEUE1,true,false,false,null);
        DefaultConsumer consume=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //空
                System.out.println("接收到消息"+new String(body));
            }
        };
        channel.basicConsume(Constants.DIRECT_QUEUE1, true,consume);
        System.out.println("接收到消息");
        Thread.sleep(2000);




    }
}
